/*     */ package com.zimbra.cs.service.mail;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.cs.mailbox.Contact;
/*     */ import com.zimbra.cs.mailbox.ContactGroup.MigrateContactGroup;
/*     */ import com.zimbra.cs.mailbox.MailServiceException;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.mailbox.util.TagUtil;
/*     */ import com.zimbra.cs.mime.ParsedContact;
/*     */ import com.zimbra.cs.service.FileUploadServlet;
/*     */ import com.zimbra.cs.service.FileUploadServlet.Upload;
/*     */ import com.zimbra.cs.service.formatter.ContactCSV;
/*     */ import com.zimbra.cs.service.formatter.ContactCSV.ParseException;
/*     */ import com.zimbra.cs.service.util.ItemId;
/*     */ import com.zimbra.cs.service.util.ItemIdFormatter;
/*     */ import com.zimbra.soap.JaxbUtil;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import com.zimbra.soap.mail.message.ImportContactsRequest;
/*     */ import com.zimbra.soap.mail.message.ImportContactsResponse;
/*     */ import com.zimbra.soap.mail.type.Content;
/*     */ import com.zimbra.soap.mail.type.ImportContact;
/*     */ import java.io.BufferedReader;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStreamReader;
/*     */ import java.io.StringReader;
/*     */ import java.util.ArrayList;
/*     */ import java.util.LinkedList;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ImportContacts
/*     */   extends MailDocumentHandler
/*     */ {
/*  61 */   private static final String[] TARGET_FOLDER_PATH = { "l" };
/*     */   
/*     */   protected String[] getProxiedIdPath(Element request)
/*     */   {
/*  65 */     return TARGET_FOLDER_PATH;
/*     */   }
/*     */   
/*     */   protected boolean checkMountpointProxy(Element request)
/*     */   {
/*  70 */     return true;
/*     */   }
/*     */   
/*  73 */   private String DEFAULT_FOLDER_ID = "7";
/*     */   
/*     */   public Element handle(Element request, Map<String, Object> context) throws ServiceException
/*     */   {
/*  77 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  78 */     Mailbox mbox = getRequestedMailbox(zsc);
/*  79 */     OperationContext octxt = getOperationContext(zsc, context);
/*  80 */     ItemIdFormatter ifmt = new ItemIdFormatter(zsc);
/*     */     
/*  82 */     ImportContactsRequest req = (ImportContactsRequest)JaxbUtil.elementToJaxb(request);
/*  83 */     String ct = req.getContentType();
/*  84 */     if (!"csv".equals(ct))
/*  85 */       throw ServiceException.INVALID_REQUEST("unsupported content type: " + ct, null);
/*  86 */     String folder = req.getFolderId();
/*  87 */     if (folder == null)
/*  88 */       folder = this.DEFAULT_FOLDER_ID;
/*  89 */     ItemId iidFolder = new ItemId(folder, zsc);
/*     */     
/*  91 */     String format = req.getCsvFormat();
/*  92 */     String locale = req.getCsvLocale();
/*  93 */     Content reqContent = req.getContent();
/*  94 */     List<Map<String, String>> contacts = null;
/*  95 */     List<FileUploadServlet.Upload> uploads = null;
/*  96 */     BufferedReader reader = null;
/*  97 */     String attachment = reqContent.getAttachUploadId();
/*     */     try {
/*  99 */       if (attachment == null)
/*     */       {
/* 101 */         String text = StringUtil.lfToCrlf(reqContent.getValue());
/* 102 */         reader = new BufferedReader(new StringReader(text));
/*     */       } else {
/* 104 */         reader = parseUploadedContent(zsc, attachment, uploads = new ArrayList());
/*     */       }
/*     */       
/* 107 */       contacts = ContactCSV.getContacts(reader, format, locale);
/* 108 */       reader.close();
/*     */     } catch (IOException e) {
/* 110 */       throw MailServiceException.UNABLE_TO_IMPORT_CONTACTS(e.getMessage(), e);
/*     */     } catch (ContactCSV.ParseException e) {
/* 112 */       throw MailServiceException.UNABLE_TO_IMPORT_CONTACTS(e.getMessage(), e);
/*     */     } finally {
/* 114 */       if (reader != null)
/* 115 */         try { reader.close();
/*     */         } catch (IOException e) {}
/* 117 */       if (attachment != null) {
/* 118 */         FileUploadServlet.deleteUploads(uploads);
/*     */       }
/*     */     }
/*     */     
/* 122 */     List<ItemId> idsList = ImportCsvContacts(octxt, mbox, iidFolder, contacts);
/*     */     
/*     */ 
/* 125 */     ImportContactsResponse resp = new ImportContactsResponse();
/* 126 */     ImportContact impCntct = new ImportContact();
/*     */     
/* 128 */     for (ItemId iid : idsList) {
/* 129 */       impCntct.addCreatedId(iid.toString(ifmt));
/*     */     }
/* 131 */     impCntct.setNumImported(contacts.size());
/* 132 */     resp.setContact(impCntct);
/*     */     
/* 134 */     return zsc.jaxbToElement(resp);
/*     */   }
/*     */   
/*     */   private static BufferedReader parseUploadedContent(ZimbraSoapContext lc, String attachId, List<FileUploadServlet.Upload> uploads) throws ServiceException
/*     */   {
/* 139 */     FileUploadServlet.Upload up = FileUploadServlet.fetchUpload(lc.getAuthtokenAccountId(), attachId, lc.getAuthToken());
/* 140 */     if (up == null) {
/* 141 */       throw MailServiceException.NO_SUCH_UPLOAD(attachId);
/*     */     }
/*     */     
/* 144 */     uploads.add(up);
/*     */     try {
/* 146 */       return new BufferedReader(new InputStreamReader(up.getInputStream(), "UTF-8"));
/*     */     } catch (IOException e) {
/* 148 */       throw ServiceException.FAILURE(e.getMessage(), e);
/*     */     }
/*     */   }
/*     */   
/*     */   public static List<ItemId> ImportCsvContacts(OperationContext oc, Mailbox mbox, ItemId iidFolder, List<Map<String, String>> csvContacts) throws ServiceException
/*     */   {
/* 154 */     List<ItemId> createdIds = new LinkedList();
/* 155 */     for (Map<String, String> contact : csvContacts) {
/* 156 */       String[] tags = TagUtil.decodeTags(ContactCSV.getTags(contact));
/* 157 */       Contact c = mbox.createContact(oc, new ParsedContact(contact), iidFolder.getId(), tags);
/* 158 */       createdIds.add(new ItemId(c));
/* 159 */       ContactGroup.MigrateContactGroup mcg = new ContactGroup.MigrateContactGroup(mbox);
/* 160 */       mcg.migrate(c);
/*     */     }
/* 162 */     return createdIds;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/mail/ImportContacts.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */